package utils
{
import core.StageProxy;

import flash.display.DisplayObject;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Dictionary;

import isoengine.drop.DropItem;

import mx.utils.UIDUtil;

public class DropUtil
{
    public var id:String = UIDUtil.createUID();
    
    private var vx:Number = 1;	
    
    private var vy:Number = -7;
    
    private var direction:int = -1;
    
    private var gravity:Number = 0.8;
    
    private var bounce:Number = -0.5;
    
    private var target:DropItem;
    
    private var bottom:Number;
    
    private var onComplete:Function;
    
    private static var objects:Dictionary = new Dictionary(true);
    
    public static function start(value:DropItem, 
                                 vx:Number, vy:Number, 
                                 direction:Number, 
                                 gravity:Number, 
                                 bounce:Number, 
                                 bottom:Number, 
                                 onComplete:Function = null):void
    {
        var util:DropUtil = new DropUtil();
        objects[util.id] = util;
        util.start(value, vx, vy, direction, gravity, bounce, bottom, onComplete);
    }
    
    public static function clear():void
    {
        for each (var util:DropUtil in objects)
        {
            util.dispose();
        }
        objects = new Dictionary(true);
    }
    
    public function start(value:DropItem, 
                          vx:Number, vy:Number, 
                          direction:Number, 
                          gravity:Number, 
                          bounce:Number, 
                          bottom:Number, 
                          onComplete:Function = null):void
    {
        target = value;
        this.vx = vx;
        this.vy = vy;
        this.direction = direction;
        this.gravity = gravity;
        this.bounce = bounce;
        this.bottom = bottom;
        this.onComplete = onComplete;
        StageProxy.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    public function dispose():void
    {
        StageProxy.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    
    private function onEnterFrame(event:Event):void
    {		
        vy += gravity;
        
        target.renderOffset.y += vy;
        
        target.renderOffset.x += direction * vx;
        
        if( target.renderOffset.y > bottom)
        {
            target.renderOffset.y = bottom ;
            
            vy *= bounce;
            
            if(vy > -1)
            {				
                StageProxy.stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                
                delete objects[id];
                
                if (onComplete != null)
                {
                    onComplete();
                }
            } 
            
        }
    }
}
}